package com.busi.wms.controller;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.busi.wms.model.WmsStock;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import com.jfinal.plugin.activerecord.Record;
import com.zcurd.common.tool.DBTool;
import com.zcurd.common.util.StringUtil;
import com.zcurd.controller.BaseController;
import com.zcurd.ext.render.csv.CsvRender;

/**
 * 盘点单主子表处理
 */
public class WmsStockController extends BaseController {
	
	public void listPage() {
		setAttr("dictDatapd_type", WmsStock.me.getDictDatapd_type());
		setAttr("dictDatastorage_no", WmsStock.me.getDictDatastorage_no());
		setAttr("dictDataPd_status", WmsStock.me.getDictDatapd_status());
		render("list.html");
	}
	
	public void listData() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_stock", properties, symbols, values, orderBy, getPager());
		Map<String, Object> dictDatapd_type = WmsStock.me.getDictDatapd_type();
		for(Record record : list) {
			String fieldName = "pd_type";
			if(dictDatapd_type.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatapd_type.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDatastorage_no = WmsStock.me.getDictDatastorage_no();
		for(Record record : list) {
			String fieldName = "storage_no";
			if(dictDatastorage_no.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatastorage_no.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDatapd_status = WmsStock.me.getDictDatapd_status();
		for(Record record : list) {
			String fieldName = "pd_status";
			if(dictDatapd_status.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatapd_status.get(record.get(fieldName).toString()));
			}
		}
		
		renderDatagrid(
			list, 
			DBTool.countByMultProperties("wms_stock", properties, symbols, values)
		);
	}
	
	//增加页面
	public void addPage() {
		setAttr("dictDatapd_type", WmsStock.me.getDictDatapd_type());
		setAttr("dictDatastorage_no", WmsStock.me.getDictDatastorage_no());		
		setAttr("pd_name", PropKit.get("comp_name")+"盘库单");
		render("add.html");
	}
	
	//增加
	public void add() {
		getModel(WmsStock.class, "model").save();
		
		addOpLog("[盘点单主表] 增加");
		renderSuccess();
	}
	
	//修改页面
	public void updatePage() {
		setAttr("dictDatapd_type", WmsStock.me.getDictDatapd_type());
		setAttr("dictDatastorage_no", WmsStock.me.getDictDatastorage_no());
		setAttr("model", WmsStock.me.findById(getPara("id")));
		render("update.html");
	}
	
	//修改
	public void update() {
		WmsStock model = WmsStock.me.findById(getPara("id"));
		model.set("pd_no", getPara("model.pd_no"));
		model.set("pd_time", getPara("model.pd_time"));
		model.set("pd_user", getPara("model.pd_user"));
		model.set("pd_type", getPara("model.pd_type"));
		model.set("storage_no", getPara("model.storage_no"));
		model.set("pd_name", getPara("model.pd_name"));
		model.update();
		addOpLog("[盘点单主表] 修改");
		renderSuccess();
	}
	
	//删除
	public void delete() {
		WmsStock.me.findById(getPara("id")).delete();
		
		addOpLog("[盘点单主表] 删除");
		renderSuccess();
	}
	
	//详情页面
	public void detailPage() {
		WmsStock model = WmsStock.me.findById(getParaToInt("id"));
		Map<String, Object> dictDatapd_type = WmsStock.me.getDictDatapd_type();
		if(dictDatapd_type.get(model.get("pd_type").toString()) != null) {
			model.set("pd_type", dictDatapd_type.get(model.get("pd_type").toString()));
		}
		Map<String, Object> dictDatastorage_no = WmsStock.me.getDictDatastorage_no();
		if(dictDatastorage_no.get(model.get("storage_no").toString()) != null) {
			model.set("storage_no", dictDatastorage_no.get(model.get("storage_no").toString()));
		}
		setAttr("model", model);
		render("detail.html");
	}
	
	//导出csv
	public void exportCsv() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}
		
		List<Record> list = DBTool.findByMultProperties("wms_stock", properties, symbols, values);
		Map<String, Object> dictDatapd_type = WmsStock.me.getDictDatapd_type();
		for(Record record : list) {
			String fieldName = "pd_type";
			if(dictDatapd_type.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatapd_type.get(record.get(fieldName).toString()));
			}
		}
		Map<String, Object> dictDatastorage_no = WmsStock.me.getDictDatastorage_no();
		for(Record record : list) {
			String fieldName = "storage_no";
			if(dictDatastorage_no.get(record.get(fieldName).toString()) != null) {
				record.set(fieldName, dictDatastorage_no.get(record.get(fieldName).toString()));
			}
		}
		
		List<String> headers = new ArrayList<String>();
		List<String> clomuns = new ArrayList<String>();
		headers.add("盘点单号");
		clomuns.add("pd_no");
		headers.add("盘点时间");
		clomuns.add("pd_time");
		headers.add("盘点人");
		clomuns.add("pd_user");
		headers.add("盘点类型");
		clomuns.add("pd_type");
		headers.add("仓库编号");
		clomuns.add("storage_no");
		headers.add("备注");
		clomuns.add("remark");
		
		CsvRender csvRender = new CsvRender(headers, list);
		csvRender.clomuns(clomuns);
		csvRender.fileName("盘点单主表");
		
		addOpLog("[盘点单主表] 导出cvs");
		render(csvRender);
	}
	//盘库单明细页面
	public void listDetailPage() {
		setAttr("pd_id",getParaToInt("pd_id"));
		render("stockDetail.html");
	}
	//盘库单明细列表数据
	public void listDetailData() {
		Object[] queryParams = getQueryParams();
		String[] properties = (String[]) queryParams[0];
		String[] symbols = (String[]) queryParams[1];
		Object[] values = (Object[]) queryParams[2];
		
		String orderBy = getOrderBy();
		if(StringUtil.isEmpty(orderBy)) {
			orderBy = "id desc";
		}		
		List<Record> list = DBTool.findByMultProperties("wms_stock_detail", properties, symbols, values, orderBy, getPager());
		
		renderDatagrid(
			list, 
			DBTool.countByMultProperties("wms_stock_detail", properties, symbols, values)
		);
	}
	//调用盘库存储过程
	public void panKu(){
		int pd_id =getParaToInt("id");
		int storage_no=WmsStock.me.getStorageIdByname(getPara("storage_no"));
		Db.execute(new ICallback(){
			@Override
			public Object call(Connection conn) throws SQLException {
				  CallableStatement proc = conn.prepareCall("{ call sp_wms_stock_proc(?, ?,?) }"); 
				  proc.setInt(1, pd_id);
			      proc.setInt(2, storage_no);
			      proc.setBoolean(3, false);
			      return proc.execute();				
			}
		});		
		renderSuccess();;
	}
}
